package software.literature.db;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import software.literature.model.bean.BBGData;
import software.literature.model.bean.BBGRecords;
import software.literature.model.bean.Journal;
import software.literature.model.bean.Monograph;
import software.literature.model.bean.Proceedings;
import software.literature.model.bean.TechnicalReport;
import software.literature.model.bean.ThesesOrDissertation;
import software.literature.model.bean.URLType;

public class BBGDao extends DBDao {
	public BBGDao(String currentDB) {
		super(currentDB);

	}

	public boolean insertBBG(BBGData data) {
		try {
			getConnection();
			preStm = connection
					.prepareStatement("insert into bbg(data,url,tree_id,type) values(?,?,?,?)");
			preStm.setString(1, data.getData());
			preStm.setString(2, data.getUrl());
			preStm.setInt(3, data.getTreeId());
			preStm.setInt(4, data.getType());
			int re = preStm.executeUpdate();
			preStm = connection.prepareStatement("select max(id) from bbg");
			result = preStm.executeQuery();
			if (result.next()) {
				data.setId(result.getInt(1));
			}
			closeConnection();
			if (1 == re) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;

	}
	public boolean updateBBGExceptNote(BBGData bbg) {
		try {
			getConnection();
			preStm = connection
					.prepareStatement("update bbg set data=? ,url=? where id=?");
			preStm.setString(1, bbg.getData());
			preStm.setString(2, bbg.getUrl());
			preStm.setInt(3, bbg.getId());
			int re = preStm.executeUpdate();
			closeConnection();
			if (1 == re) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	public boolean deleteBBGs(List<BBGData> bbgs, int[] is) {
		try {
			getConnection();
			connection.setAutoCommit(false);
			preStm = connection.prepareStatement("delete from bbg where id=?");
			for (int i = 0; i < is.length; i++) {
				preStm.setInt(1, bbgs.get(is[i]).getId());
				preStm.addBatch();
			}
			connection.setAutoCommit(false);
			preStm.executeBatch();
			connection.setAutoCommit(true);
			closeConnection();
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	public boolean updateNote(BBGData bbg) {
		// TODO Auto-generated method stub
		try {
			getConnection();
			preStm = connection
					.prepareStatement("update bbg set note_text=? ,note_url=? where id=?");
			preStm.setString(1, bbg.getNoteText());
			preStm.setString(2, bbg.getNoteUrl());
			preStm.setInt(3, bbg.getId());
			int re = preStm.executeUpdate();
			closeConnection();
			if (1 == re) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	// fetch all bbg records in the specific database
	public ArrayList<BBGData> queryBBGdata() {
		ArrayList<BBGData> bbgs = new ArrayList<BBGData>();
		try {
			getConnection();
			preStm = connection.prepareStatement("select * from  bbg");
			result = preStm.executeQuery();
			while (result.next()) {
				int type = result.getInt("type");
				BBGData bbgData = null;
				switch (type) {
				case 0: {
					bbgData = new Monograph();
					break;
				}
				case 1: {
					bbgData = new Journal();
					break;

				}
				case 2: {
					bbgData = new Proceedings();
					break;
				}
				case 3: {
					bbgData = new TechnicalReport();
					break;
				}
				case 4: {
					bbgData = new ThesesOrDissertation();
					break;
				}
				case 5: {
					bbgData = new URLType();
					break;
				}
				}
				bbgData.setId(result.getInt("id"));
				bbgData.parse(result.getString("data"));
				bbgData.setUrl(result.getString("url"));
				bbgData.setTreeId(result.getInt("tree_id"));
				bbgData.setType(result.getInt("type"));
				bbgData.setNoteText(result.getString("note_text"));
				bbgData.setNoteUrl(result.getString("note_url"));
				bbgs.add(bbgData);
			}
			closeConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bbgs;
	}

	public boolean moveBBG(int id, List<BBGData> bbgs) {
		try {
			getConnection();
			preStm = connection
					.prepareStatement("update  bbg set tree_id=? where id=?");
			for (int i = 0; i < bbgs.size(); i++) {
				preStm.setInt(1, id);
				preStm.setInt(2, bbgs.get(i).getId());
				preStm.addBatch();
			}
			connection.setAutoCommit(false);
			int[] count = preStm.executeBatch();
			connection.setAutoCommit(true);
			closeConnection();
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	// delete reference from specific tree item
	public boolean removeBBGs(List<BBGData> bbgs, int[] selected) {
		// TODO Auto-generated method stub
		try {
			getConnection();
			preStm = connection
					.prepareStatement("update  bbg set tree_id=? where id=?");
			for (int i = 0; i < selected.length; i++) {

				preStm.setInt(1, bbgs.get(selected[i]).getTreeId());
				preStm.setInt(2, bbgs.get(selected[i]).getId());
				preStm.addBatch();
			}
			closeConnection();
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
}
